package com.financial.business.balance.config;

import com.alibaba.fastjson.JSON;
import com.financial.business.balance.dto.FinancialSecurityConfDTO;
import com.financial.business.balance.utils.HttpClientUtils;
import com.financial.business.balance.utils.MapUtils;
import com.financial.business.balance.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //够样平台获取token
 * @Date 2019/12/6 10:25
 */
@Component
public class GYAccessTokenConfig {
    public static final Logger logger = LoggerFactory.getLogger(GYAccessTokenConfig.class);

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    private RedisUtils redisUtils;

    /*获取token*/
    public String getGYAccessToken(String platformName, String passWord, String secret, String sign) {
        logger.info("GYAccessTokenConfig.getGYAccessToken()-----获取够样余额中心的参数信息:platformName：" + platformName + "----passWord:" + secret + "---sign:" + sign);
        String result = null;
        try {
            FinancialSecurityConfDTO financialSecurityConfDTO = new FinancialSecurityConfDTO();
            financialSecurityConfDTO.setPlatformName(platformName);
            financialSecurityConfDTO.setPassWord(passWord);
            financialSecurityConfDTO.setSecret(secret);
            financialSecurityConfDTO.setSign(sign);
            result = httpClientUtils.doPost(UrlConfig.ACCESSTOKEN_URL, MapUtils.objectToMap(financialSecurityConfDTO));
            Map<String, Object> map = (Map<String, Object>) JSON.parseObject(result, Map.class);
            String accessToken = (String) map.get("accessToken");
            int time = (int) map.get("time");
            /*保存到redis数据库*/
            redisUtils.setKey("financialAccessToken", accessToken, time - 10);
            logger.info("GYAccessTokenConfig.getGYAccessToken()-----获取够样余额中心的accessToken:" + accessToken);
            return accessToken;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("GYAccessTokenConfig.getGYAccessToken()-----获取够样余额中心的accessToken失败:" + e.getMessage());
        }
        return result;
    }
}
